package datebase;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class sdafasfd extends JFrame {

	private JPanel contentPane;
	private JLabel label;
	private JLabel label_1;
	private JButton btnNewButton;
	private JButton button;
	private JButton button_1;
	private JLabel lblNewLabel;
	private JLabel la1;
	private JLabel la2;
	private JLabel la3;
	private JLabel la4;
	int a, uw, d, ul, cw, cl, user;
	static String driver = "oracle.jdbc.driver.OracleDriver";
	static String url = "jdbc:oracle:thin:@192.168.10.129:1521:XE";
	Connection con;
	private JLabel la5;
	private JButton btnNewButton_1;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					sdafasfd frame = new sdafasfd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public sdafasfd() {
		try {
			Class.forName(driver).newInstance();
			System.out.println("드라이버가 로딩되었습니다.");

			con = DriverManager.getConnection(url, "hr", "hr");
			System.out.println("데이터베이스가 정상적으로 연결됨");
		} catch (Exception ev) {
			ev.getMessage();
		}
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 506, 643);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLabel());
		contentPane.add(getLabel_1());
		contentPane.add(getBtnNewButton());
		contentPane.add(getButton());
		contentPane.add(getButton_1());
		contentPane.add(getLblNewLabel());
		contentPane.add(getLa1());
		contentPane.add(getLa2());
		contentPane.add(getLa3());
		contentPane.add(getLa4());
		contentPane.add(getLa5());
		contentPane.add(getBtnNewButton_1());
		re();

	}

	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("");
			label.setIcon(new ImageIcon(
					"C:\\Users\\jhta\\Pictures\\이미지\\은규형1.jpg"));
			label.setBounds(12, 105, 210, 140);
		}
		return label;
	}

	public void re() {
		String sql = "select * from game";
		try {
			PreparedStatement pstmt = con.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();

			System.out.println("여긴됨");
			if (rs.next()) {

				uw = rs.getInt(1);
				d = rs.getInt(2);
				ul = rs.getInt(3);
				cw = rs.getInt(4);
				cl = rs.getInt(5);

			}
			pstmt.close();
			la3.setText(uw + "승  " + ul + "패");
			la4.setText(cw + "승  " + cl + "패");
			la5.setText(d + "무");
		} catch (Exception ev) {
			ev.getMessage();
		}
	}

	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("");
			label_1.setIcon(new ImageIcon(
					"C:\\Users\\jhta\\Pictures\\이미지\\은규형2.jpg"));
			label_1.setBounds(268, 105, 210, 140);
		}
		return label_1;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					la1.setIcon(new ImageIcon(
							"C:\\Users\\jhta\\Pictures\\이미지\\가위.png"));
					com();
					user = 0;
				}
			});
			btnNewButton.setBackground(Color.WHITE);
			btnNewButton.setIcon(new ImageIcon(
					"C:\\Users\\jhta\\Pictures\\이미지\\가위1.png"));
			btnNewButton.setBounds(12, 425, 135, 160);
		}
		return btnNewButton;
	}

	private JButton getButton() {
		if (button == null) {
			button = new JButton("");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					la1.setIcon(new ImageIcon(
							"C:\\Users\\jhta\\Pictures\\이미지\\보.png"));
					user = 2;
					com();
				}
			});
			button.setBackground(Color.WHITE);
			button.setIcon(new ImageIcon(
					"C:\\Users\\jhta\\Pictures\\이미지\\보1.png"));
			button.setBounds(343, 425, 135, 160);
		}
		return button;
	}

	private JButton getButton_1() {
		if (button_1 == null) {
			button_1 = new JButton("");
			button_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					la1.setIcon(new ImageIcon(
							"C:\\Users\\jhta\\Pictures\\이미지\\바위.png"));
					user = 1;
					com();
				}
			});
			button_1.setIcon(new ImageIcon(
					"C:\\Users\\jhta\\Pictures\\이미지\\바위1.png"));
			button_1.setBackground(Color.WHITE);
			button_1.setBounds(177, 425, 135, 160);
		}
		return button_1;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("VS");
			lblNewLabel.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 60));
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(192, 255, 113, 160);
		}
		return lblNewLabel;
	}

	private JLabel getLa1() {
		if (la1 == null) {
			la1 = new JLabel("");
			la1.setBounds(12, 255, 160, 135);
		}
		return la1;
	}

	private JLabel getLa2() {
		if (la2 == null) {
			la2 = new JLabel("");
			la2.setBounds(318, 255, 160, 135);
		}
		return la2;
	}

	public void Sql(String str) {
		try {

			PreparedStatement pstmt = con.prepareStatement(str);
			pstmt.executeUpdate();

			re();
			la3.setText(uw + "승  " + ul + "패");
			la4.setText(cw + "승  " + cl + "패");
		} catch (Exception ev) {
			ev.getMessage();
		}
	}

	public void com() {
		Random rand = new Random();
		a = rand.nextInt(3);

		String drow = "update game set drow=drow+1 where num=1";
		String win = "update game set uwin=uwin+1,clo=clo+1 where num=1";
		String lose = "update game set ulose=ulose+1,cwin=cwin+1 where num=1";
		if (a == 0) {
			if (user == 0) {
				Sql(drow);
			} else if (user == 1) {
				Sql(win);
			} else {
				Sql(lose);
			}
			la2.setIcon(new ImageIcon("C:\\Users\\jhta\\Pictures\\이미지\\가위2.png"));
		} else if (a == 1) {
			if (user == 0) {
				Sql(lose);
			} else if (user == 1) {
				Sql(drow);
			} else {
				Sql(win);
			}
			la2.setIcon(new ImageIcon("C:\\Users\\jhta\\Pictures\\이미지\\바위2.png"));
		} else {
			if (user == 0) {
				Sql(win);
			} else if (user == 1) {
				Sql(lose);
			} else {
				Sql(drow);
			}
			la2.setIcon(new ImageIcon("C:\\Users\\jhta\\Pictures\\이미지\\보2.png"));
		}
	}

	private JLabel getLa3() {
		if (la3 == null) {
			la3 = new JLabel("");
			la3.setHorizontalAlignment(SwingConstants.CENTER);
			la3.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 40));
			la3.setBounds(12, 29, 189, 58);
		}
		return la3;
	}

	private JLabel getLa4() {
		if (la4 == null) {
			la4 = new JLabel("");
			la4.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 40));
			la4.setHorizontalAlignment(SwingConstants.CENTER);
			la4.setBounds(289, 29, 189, 58);
		}
		return la4;
	}

	private JLabel getLa5() {
		if (la5 == null) {
			la5 = new JLabel("");
			la5.setFont(new Font("1훈하얀고양이 R", Font.BOLD, 40));
			la5.setBounds(213, 29, 113, 58);
		}
		return la5;
	}

	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("전적 초기화");
			btnNewButton_1.setBackground(Color.WHITE);
			btnNewButton_1.setFont(new Font("나눔손글씨 펜", Font.PLAIN, 20));
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						String str = "update game set uwin=0,ulose=0,drow=0,cwin=0,clo=0 where num=1";
						PreparedStatement pstmt = con.prepareStatement(str);
						pstmt.executeUpdate();

						re();
						la3.setText(uw + "승  " + ul + "패");
						la4.setText(cw + "승  " + cl + "패");
					} catch (Exception ev) {
						ev.getMessage();
					}
				}
			});
			btnNewButton_1.setBounds(343, 10, 135, 23);
		}
		return btnNewButton_1;
	}
}
